package com.cinema.service;

import com.cinema.model.CinemaHall;
import com.cinema.model.Screening;
import com.cinema.util.JsonFileStorage;
import java.time.LocalDateTime;
import java.util.*;

public class CinemaService {
    private Map<String, CinemaHall> cinemaHalls;
    private Map<String, Screening> screenings;
    private static final String HALLS_FILE = "data/cinema_halls.json";
    private static final String SCREENINGS_FILE = "data/screenings.json";

    public CinemaService() {
        loadCinemaHalls();
        loadScreenings();
        initializeDefaultHalls();
    }

    private void loadCinemaHalls() {
        try {
            cinemaHalls = JsonFileStorage.loadFromFile(HashMap.class, HALLS_FILE);
            if (cinemaHalls == null) {
                cinemaHalls = new HashMap<>();
            }
        } catch (Exception e) {
            System.out.println("加载影厅数据失败: " + e.getMessage());
            cinemaHalls = new HashMap<>();
        }
    }

    private void loadScreenings() {
        try {
            screenings = JsonFileStorage.loadFromFile(HashMap.class, SCREENINGS_FILE);
            if (screenings == null) {
                screenings = new HashMap<>();
            }
        } catch (Exception e) {
            System.out.println("加载场次数据失败: " + e.getMessage());
            screenings = new HashMap<>();
        }
    }

    private void initializeDefaultHalls() {
        // 初始化5个放映厅
        String[] hallNames = {"黄金厅", "白银厅", "翡翠厅", "钻石厅", "宝石厅"};
        int[] rows = {8, 10, 7, 7, 8};
        int[] seatsPerRow = {10, 12, 8, 8, 9};

        for (int i = 0; i < hallNames.length; i++) {
            String hallId = "H" + (i + 1);
            if (!cinemaHalls.containsKey(hallId)) {
                CinemaHall hall = new CinemaHall(hallId, hallNames[i], rows[i], seatsPerRow[i]);
                cinemaHalls.put(hallId, hall);
            }
        }
        saveCinemaHalls();
    }

    private boolean saveCinemaHalls() {
        try {
            JsonFileStorage.saveToFile(cinemaHalls, HALLS_FILE);
            return true;
        } catch (Exception e) {
            System.out.println("保存影厅数据失败: " + e.getMessage());
            return false;
        }
    }

    private boolean saveScreenings() {
        try {
            JsonFileStorage.saveToFile(screenings, SCREENINGS_FILE);
            return true;
        } catch (Exception e) {
            System.out.println("保存场次数据失败: " + e.getMessage());
            return false;
        }
    }

    public List<CinemaHall> getAllHalls() {
        return new ArrayList<>(cinemaHalls.values());
    }

    public CinemaHall getHallById(String hallId) {
        return cinemaHalls.get(hallId);
    }

    public boolean addScreening(Screening screening) {
        if (screenings.containsKey(screening.getScreeningId())) {
            System.out.println("场次ID已存在: " + screening.getScreeningId());
            return false;
        }

        // 检查时间冲突
        for (Screening existing : screenings.values()) {
            if (existing.getHall().getHallId().equals(screening.getHall().getHallId())) {
                if (screening.getStartTime().isBefore(existing.getEndTime()) &&
                        screening.getEndTime().isAfter(existing.getStartTime())) {
                    System.out.println("时间冲突: 该影厅在该时间段已有安排");
                    return false;
                }
            }
        }

        screenings.put(screening.getScreeningId(), screening);
        return saveScreenings();
    }

    public boolean updateScreening(String screeningId, Screening updatedScreening) {
        if (!screenings.containsKey(screeningId)) {
            System.out.println("场次不存在: " + screeningId);
            return false;
        }
        screenings.put(screeningId, updatedScreening);
        return saveScreenings();
    }

    public boolean deleteScreening(String screeningId) {
        if (!screenings.containsKey(screeningId)) {
            System.out.println("场次不存在: " + screeningId);
            return false;
        }
        screenings.remove(screeningId);
        return saveScreenings();
    }

    public Screening getScreeningById(String screeningId) {
        return screenings.get(screeningId);
    }

    public List<Screening> getAllScreenings() {
        return new ArrayList<>(screenings.values());
    }

    public List<Screening> getScreeningsByDate(LocalDateTime date) {
        List<Screening> result = new ArrayList<>();
        for (Screening screening : screenings.values()) {
            if (screening.getStartTime().toLocalDate().equals(date.toLocalDate())) {
                result.add(screening);
            }
        }
        return result;
    }

    public List<Screening> getScreeningsThisWeek() {
        List<Screening> result = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime endOfWeek = now.plusDays(7);

        for (Screening screening : screenings.values()) {
            if (!screening.getStartTime().isBefore(now) &&
                    !screening.getStartTime().isAfter(endOfWeek)) {
                result.add(screening);
            }
        }
        return result;
    }
}
